import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Alice extends JFrame implements ActionListener
{
  private static final int MOD = 47154089;
  private static final int ENC = 10687;
  private static final int BASE = 36;
  //dec = 4411
  
  private static JPanel totalPane = new JPanel(new GridLayout(2, 1));
  private static JPanel fieldsPane = new JPanel(new GridLayout(0, 2));
  private static JPanel buttonPane = new JPanel();
  private static JTextField idField = new JTextField("Win-W1W1-X2X2-Y3Y3-Z4Z4");
  private static JTextField codeField = new JTextField();
  private static JButton genL = new JButton("Encrypt");
  private static JButton exit = new JButton("Exit");
  
  Alice()
  {
    super("Alice");
    
    setSize(350, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    Container con = getContentPane();
    con.add(totalPane);
    
    
    totalPane.add(fieldsPane);
    buttonPane.setAlignmentY(CENTER_ALIGNMENT);
    totalPane.add(buttonPane);
    
    fieldsPane.add(new JLabel("ID:"));
    fieldsPane.add(idField);
    fieldsPane.add(new JLabel("License Code:"));
    codeField.setEditable(false);
    fieldsPane.add(codeField);
    
    genL.setMnemonic('e');
    buttonPane.add(genL);
    genL.addActionListener(this);
    
    exit.setMnemonic('x');
    buttonPane.add(exit);
    exit.addActionListener(this);
    
    setVisible(true);
  }
  
  public void actionPerformed(ActionEvent event)
  {
    Object source = event.getSource();
    
    if (source == genL)
    {
      String contents = makeCode(idField.getText());
      
      codeField.setText(contents);  
    }
    
    else if (source == exit)
      System.exit(EXIT_ON_CLOSE);
  }
  
  public static void main(String[] args) { new Alice(); }
  
  public static String makeCode(String id)
  {
    String[] bits = id.split("-");
    String lic = "";
    
    for (int i = 0, k = 0; i < bits.length; i++)
    {
      if (bits[i].length() == 4)
      {
        int hash = 7;
        for (int j = 0; j < bits[i].length(); j++)
          hash = hash * 43 + bits[i].charAt(j);

        BigInteger c = BigInteger.valueOf(hash).modPow(BigInteger.valueOf(ENC), BigInteger.valueOf(MOD));
        lic = lic + Integer.toString(c.intValue(), BASE);
        k++;
        
        if (k != 4)
          lic = lic + "-";
      }
    }
    
    return lic;
  }
}
